package dto

import (
	"github.com/honwee/awm/internal/model"
	"time"
)

// CreateWorkflowRunRequest 创建工作流运行记录请求
type CreateWorkflowRunRequest struct {
	WorkflowRunID string `json:"workflow_run_id" binding:"required"`
	WorkflowID    string `json:"workflow_id" binding:"required"`
}

// UpdateWorkflowRunRequest 更新工作流运行记录请求
type UpdateWorkflowRunRequest struct {
	Status     string     `json:"status" binding:"required,oneof=running succeeded failed"`
	FinishedAt *time.Time `json:"finished_at,omitempty"`
}

// WorkflowRunResponse 工作流运行记录响应
type WorkflowRunResponse struct {
	ID            uint64     `json:"id"`
	WorkflowRunID string     `json:"workflow_run_id"`
	WorkflowID    string     `json:"workflow_id"`
	UserID        string     `json:"user_id"`
	Status        string     `json:"status"`
	StartedAt     time.Time  `json:"started_at"`
	FinishedAt    *time.Time `json:"finished_at,omitempty"`
	CreatedAt     time.Time  `json:"created_at"`
}

// WorkflowRunListResponse 工作流运行记录列表响应
type WorkflowRunListResponse struct {
	Runs       []WorkflowRunResponse `json:"runs"`
	Pagination PaginationResponse    `json:"pagination"`
}

// FromModel 从模型转换为响应
func (r *WorkflowRunResponse) FromModel(run *model.WorkflowRun) {
	r.ID = run.ID
	r.WorkflowRunID = run.WorkflowRunID
	r.WorkflowID = run.WorkflowID
	r.UserID = run.UserID
	r.Status = run.Status
	r.StartedAt = run.StartedAt
	r.FinishedAt = run.FinishedAt
	r.CreatedAt = run.CreatedAt
}

// ToModel 将创建请求转换为模型
func (r *CreateWorkflowRunRequest) ToModel(userID string) *model.WorkflowRun {
	return &model.WorkflowRun{
		WorkflowRunID: r.WorkflowRunID,
		WorkflowID:    r.WorkflowID,
		UserID:        userID,
		Status:        model.StatusRunning,
		StartedAt:     time.Now(),
	}
}

// FromModelList 从模型列表转换为响应
func (r *WorkflowRunListResponse) FromModelList(runs []model.WorkflowRun, page, pageSize int, total int64) {
	r.Runs = make([]WorkflowRunResponse, len(runs))
	for i, run := range runs {
		r.Runs[i] = WorkflowRunResponse{}
		r.Runs[i].FromModel(&run)
	}
	r.Pagination.FromParams(page, pageSize, total)
}
